package br.com.ufpr.sgp.pages;

import javax.ejb.EJB;

import org.apache.wicket.PageReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.wicketstuff.annotation.mount.MountPath;

import br.com.sebrae.pr.sgp.entities.Usuario;
import br.com.sebrae.pr.sgp.services.UsuarioServiceRemote;
import br.com.sgp.util.handling.ErrorHandlingAjaxButton;
import br.com.sgp.util.handling.ErrorHandlingAjaxLink;
import br.com.sgp.util.handling.ErrorHandlingForm;
import br.com.ufpr.sgp.util.geral.JavascriptUtils;
import br.com.ufpr.sgp.util.session.CustomSession;
import br.com.ufpr.util.base.BasePage;
import br.com.ufpr.util.feedback.IReceiveFeedbackPage;

//@Protected
@MountPath("AlterarDadosPessoais")
public class AlterarDadosPessoaisPage extends BasePage {
    private static final long serialVersionUID = 8919904497126699934L;

    @EJB
    private UsuarioServiceRemote usuarioServiceRemote;

    public AlterarDadosPessoaisPage(final ModalAlterarDadosPessoais modalAlterarDadosPessoais, final PageReference pageReference) {
        super();

        final ModalAlterarSenha modalAlterarSenha = new ModalAlterarSenha("modalAlterarSenha",
                AlterarDadosPessoaisPage.this.getPageReference()) {
            private static final long serialVersionUID = 4368126596716297050L;

            @Override
            public void onClose(AjaxRequestTarget target) {
                atualizarFeedbackPanel(target);
            }
        };
        add(modalAlterarSenha);

        final IModel<Usuario> usuModel = new LoadableDetachableModel<Usuario>() {

            @Override
            protected Usuario load() {
                return usuarioServiceRemote.pesquisarPorLogin(CustomSession.get().getUsuario().getNomeUsuario());
            }
        };

        final ErrorHandlingForm<Usuario> usuForm = new ErrorHandlingForm<Usuario>("usuForm", new CompoundPropertyModel<Usuario>(usuModel));
        usuForm.add(new TextField<String>("nmeUsuario").setLabel(new ResourceModel("alterarDadosPessoais.nmeUsuario")).setRequired(true)
                .setEnabled(false));
        usuForm.add(new TextField<String>("nmeLogin").setLabel(new ResourceModel("alterarDadosPessoais.nmeLogin")).setRequired(true)
                .setEnabled(false));
        usuForm.add(new TextField<String>("desEmail").setLabel(new ResourceModel("alterarDadosPessoais.desEmail"))
                .add(EmailAddressValidator.getInstance()).setRequired(true));
        final TextField<String> numTelefone = new TextField<String>("numTelefone");
        numTelefone.setLabel(new ResourceModel("alterarDadosPessoais.numTelefone"));
        numTelefone.setRequired(true);
        JavascriptUtils.addMask(numTelefone, "(99) 9999-9999");
        usuForm.add(numTelefone);

        usuForm.add(new ErrorHandlingAjaxButton("btnVoltar") {
            private static final long serialVersionUID = -2828403591018284992L;

            @Override
            protected void onSubmitted(AjaxRequestTarget target) {
                if (modalAlterarDadosPessoais != null) {
                    modalAlterarDadosPessoais.close(target);

                } else {
                    target.appendJavaScript("window.history.back();");
                }
            }

        }.setDefaultFormProcessing(false));

        usuForm.add(new ErrorHandlingAjaxLink("lnkAlterarSenha") {
            private static final long serialVersionUID = -3724019666940901015L;

            @Override
            public void onClicked(AjaxRequestTarget target) {
                modalAlterarSenha.show(target);
            }

        });

        usuForm.add(new ErrorHandlingAjaxButton("btnSalvar") {
            private static final long serialVersionUID = -4188037291706068843L;

            @Override
            protected void onSubmitted(AjaxRequestTarget target) {
                setUsuarioLogadoDto(usuModel);
                usuarioServiceRemote.salvarOuAtualizar(usuModel.getObject());
                if (pageReference != null && pageReference.getPage() instanceof IReceiveFeedbackPage) {
                    final IReceiveFeedbackPage irfp = (IReceiveFeedbackPage) pageReference.getPage();
                    irfp.setFeedbackMessage(new ResourceModel("alterarDadosPessoais.update.success"), target);
                }
            }

            @Override
            protected void onSucess(AjaxRequestTarget target) {
                if (modalAlterarDadosPessoais != null) {
                    modalAlterarDadosPessoais.close(target);

                } else {
                    target.appendJavaScript("window.history.back();");
                }
            }

        });

        add(usuForm);

    }
}
